/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import controllers.entity.DeTai;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hoangnv
 */
public class DeTaiDAO {
    private static PreparedStatement prst = null;
    public static List <DeTai>  getAllDeTaiByMaCanBo(int MaCanBo){
        List <DeTai> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM DeTai WHERE MaCanBo = ?");
            prst.setInt(1, MaCanBo);
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maDeTai = rs.getString("MaDeTai");
                String cap  = rs.getString("cap");
                String tenDeTai = rs.getString("TenDeTai");
                int kinhPhi = rs.getInt("KinhPhi");
                String start = rs.getString("hStart");
                String finish = rs.getString("Finish");
                String tinhTrang = rs.getString("TinhTrang");
                String ketQua = rs.getString("KetQua");
                String chuDeTai = rs.getString("ChuDeTai");
                        
                DeTai temp = new DeTai(maCanBo,maDeTai,tenDeTai,cap,kinhPhi,start,finish,chuDeTai,tinhTrang,ketQua);
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public static List <DeTai>  getAllDeTai(){
        List <DeTai> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM DeTai");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maDeTai = rs.getString("MaDeTai");
                String cap  = rs.getString("cap");
                String tenDeTai = rs.getString("TenDeTai");
                int kinhPhi = rs.getInt("KinhPhi");
                String start = rs.getString("hStart");
                String finish = rs.getString("Finish");
                String tinhTrang = rs.getString("TinhTrang");
                String ketQua = rs.getString("KetQua");
                String chuDeTai = rs.getString("ChuDeTai");
                        
                DeTai temp = new DeTai(maCanBo,maDeTai,tenDeTai,cap,kinhPhi,start,finish,chuDeTai,tinhTrang,ketQua);
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public int getCountDetai(){
        int count = 0;
        List <DeTai> list = getAllDeTai();
        for (DeTai ch : list){
            Integer i = Integer.parseInt(ch.getMaDeTai());
            if (i > count ) count = i;
        }
        return count;
    }
    public boolean InsertDeTai(DeTai X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("INSERT INTO DeTai (MaCanBo,MaDeTai,Cap,TenDeTai,hStart,Finish,TinhTrang,KetQua,KinhPhi,ChuDeTai) VALUES (?,?,?,?,?,?,?,?,?,?)");
            prst.setInt(1,X.getMaCanBo());
            prst.setString(2, X.getMaDeTai());
            prst.setString(3,X.getCap());
            prst.setString(4,X.getTenDeTai());
            prst.setString(5,X.getStart());
            prst.setString(6,X.getFinish());
            prst.setString(7, X.getTinhTrang());
            prst.setString(8, X.getKetQua());
            prst.setFloat(9,X.getKinhPhi());
            prst.setString(10, X.getChuDeTai());
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean UpdateDeTai(DeTai X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("UPDATE DeTai "
                    + "SET MaCanBo = ?,MaDeTai = ?,Cap = ?,TenDeTai = ?,hStart = ?,"
                    + "Finish = ?,TinhTrang = ?,KetQua = ?,ChuDeTai = ? WHERE MaDeTai = ?");
            prst.setInt(1,X.getMaCanBo());
            prst.setString(2, X.getMaDeTai());
            prst.setString(3,X.getCap());
            prst.setString(4,X.getTenDeTai());
            prst.setString(5,X.getStart());
            prst.setString(6,X.getFinish());
            prst.setString(7, X.getTinhTrang());
            prst.setString(8, X.getKetQua());
            prst.setString(9, X.getChuDeTai());
            prst.setString(10,X.getMaDeTai());
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean DeleteDeTaiByMaDeTai(String MaDeTai){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("DELETE FROM DeTai WHERE MaDeTai = ? ");
            prst.setString(1,MaDeTai);
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
}
